package com.carleasoft.mps.cms.business.resource.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carleasoft.mps.cms.business.article.constants.ExceptionCMSEnum;
import com.carleasoft.mps.cms.business.resource.bean.entity.LibraryEntity;
import com.carleasoft.mps.cms.business.resource.bean.entity.ReasourceCollectEntity;
import com.carleasoft.mps.cms.business.resource.bean.vo.LibraryTreeVo;
import com.carleasoft.mps.cms.business.resource.dao.LibraryMapper;
import com.carleasoft.mps.cms.business.resource.service.LibraryService;
import com.carleasoft.mps.cms.business.resource.service.ReasourceCollectService;
import com.carleasoft.mps.core.exceptions.BusinessException;
import com.carleasoft.mps.core.user.ThreadUserContext;
import com.carleasoft.mps.core.user.UserCacheData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: yangHao
 * @createDate:2023/6/6
 * @description:
 */
@Service
public class LibraryServiceImpl extends ServiceImpl<LibraryMapper, LibraryEntity> implements LibraryService {
    @Autowired
    private ReasourceCollectService reasourceCollectService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveLibrary(LibraryEntity libraryEntity) {
        boolean isOK = false;
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();
        libraryEntity.setCreateTime(LocalDateTime.now());
        libraryEntity.setCreatePersonCode(user.getUserCode());
        libraryEntity.setCreatePersonName(user.getUserName());
        if (StrUtil.isNotBlank(libraryEntity.getLibraryCode())) {
            LibraryEntity one = this.getOne(Wrappers.<LibraryEntity>lambdaQuery()
                    .eq(LibraryEntity::getLibraryCode, libraryEntity.getLibraryCode()));
            libraryEntity.setId(one.getId());
            isOK = this.updateById(libraryEntity);
        } else {
            libraryEntity.setLibraryCode(IdWorker.getIdStr());
            isOK = this.save(libraryEntity);
        }
        return isOK;
    }

    @Override
    public List<LibraryEntity> select() {
        return this.list();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(LibraryEntity libraryEntity) {
        int count = reasourceCollectService.count(Wrappers.<ReasourceCollectEntity>lambdaQuery()
                .eq(ReasourceCollectEntity::getLibrary, libraryEntity.getLibraryCode()));
        if (count > 0) {
            throw new BusinessException(ExceptionCMSEnum.EXITS_NO);
        }
        return this.remove(Wrappers.<LibraryEntity>lambdaQuery()
                .eq(LibraryEntity::getLibraryCode, libraryEntity.getLibraryCode()));
    }

    @Override
    public List<LibraryTreeVo> getTree() {
        List<LibraryTreeVo> libraryTreeVos = new ArrayList<>();
        List<LibraryEntity> libraryEntities = this.list();
        List<String> codes = libraryEntities.stream().map(LibraryEntity::getLibraryCode).collect(Collectors.toList());
        List<ReasourceCollectEntity> reasourceCollectEntities = reasourceCollectService.list(Wrappers.<ReasourceCollectEntity>lambdaQuery()
                .in(ReasourceCollectEntity::getLibrary, codes));
        Map<String, List<ReasourceCollectEntity>> map = new HashMap<>();
        if (CollUtil.isNotEmpty(reasourceCollectEntities)) {
            map = reasourceCollectEntities.stream().collect(Collectors.groupingBy(ReasourceCollectEntity::getLibrary));
        }
        for (LibraryEntity libraryEntity : libraryEntities) {
            LibraryTreeVo libraryTreeVo = new LibraryTreeVo();
            libraryTreeVo.setType(1);
            libraryTreeVo.setImage(libraryEntity.getImage());
            libraryTreeVo.setCode(libraryEntity.getLibraryCode());
            libraryTreeVo.setName(libraryEntity.getLibraryName());
            libraryTreeVo.setIsShow(libraryEntity.getIsShow());
            libraryTreeVo.setCreateTime(libraryEntity.getCreateTime());
            libraryTreeVo.setCreatePersonName(libraryEntity.getCreatePersonName());
            libraryTreeVo.setCreatePersonCode(libraryEntity.getCreatePersonCode());
            if (map.containsKey(libraryEntity.getLibraryCode())) {
                List<ReasourceCollectEntity> collectEntities = map.get(libraryEntity.getLibraryCode());
                List<LibraryTreeVo> libraryTreeVoList = new ArrayList<>();
                for (ReasourceCollectEntity collectEntity : collectEntities) {
                    LibraryTreeVo libraryTree = new LibraryTreeVo();
                    libraryTree.setType(2);
                    libraryTree.setParentCode(libraryTreeVo.getCode());
                    libraryTree.setCode(collectEntity.getCategoryCode());
                    libraryTree.setName(collectEntity.getCategoryName());
                    libraryTree.setCreateTime(collectEntity.getCreateTime());
                    libraryTree.setCreatePersonCode(collectEntity.getCreatePersonCode());
                    libraryTree.setCreatePersonName(collectEntity.getCreatePersonName());
                    libraryTreeVoList.add(libraryTree);
                }
                libraryTreeVo.setChildren(libraryTreeVoList);
            }
            libraryTreeVos.add(libraryTreeVo);
        }
        return libraryTreeVos;
    }
}
